using System;
using UnityEngine;
using ASFU.Exception;

namespace ASFU.Library
{
	public static class LibraryFunction
	{
		public static void RegFunctions()
		{			
			Util.Reg("trace", Util.CreateFunction(new trace()));
			Util.Reg("importType", Util.CreateFunction(new import_type()));
			Util.Reg("getQualifiedClassName", Util.CreateFunction(new getQualifiedClassName()));
			Util.Reg("getQualifiedSuperclassName", Util.CreateFunction(new getQualifiedSuperclassName()));
			Util.Reg("getTimer", Util.CreateFunction(new getTimer()));
			Util.Reg("getDefinitionByName", Util.CreateFunction(new getDefinitionByName()));
		}
		private class getQualifiedClassName : ScorpioHandle
		{
			public object Call(ScriptObject[] parameters)
			{
				ScriptClass scriptClass = null;
				if(parameters[0].IsObject)
				{
					scriptClass = ((ScriptASObject)parameters[0]).Class;
				}
				else if(parameters[0].IsClass)
				{
					scriptClass = (ScriptClass)parameters[0];
				}
				else if(parameters[0].IsFunction)
				{
					return "builtin.as$0::MethodClosure";
				}
				else if(parameters[0].IsArray)
				{
					return "Array";
				}
				else if(parameters[0].IsString)
				{
					return "String";
				}
				else if(parameters[0].IsBoolean)
				{
					return "Boolean";
				}
				else if(parameters[0].IsNumber)
				{
					int result = 0;
					return Util.isInt(parameters[0].ObjectValue,out result)?"int":"Number";
				}
				if(scriptClass != null)
				{
					string name = Util.IsNullOrEmpty(scriptClass.className)?"Object":scriptClass.className;
					string classPath = Util.IsNullOrEmpty(scriptClass.package)?scriptClass.className:scriptClass.package+":"+scriptClass.className;
					return classPath;
				}
				else
				{
					return null;
				}
			}
		}
		private class getQualifiedSuperclassName:ScorpioHandle
		{
			public object Call(ScriptObject[] parameters)
			{
				ScriptClass scriptClass = null;
				if(parameters[0].IsObject)
				{
					scriptClass = ((ScriptASObject)parameters[0]).parent.Class;
				}
				else if(parameters[0].IsClass)
				{
					scriptClass = ((ScriptClass)parameters[0]).prototype.parent.Class;
				}
				else if(parameters[0].IsFunction)
				{
					return "Function";
				}
				if(scriptClass != null)
				{
					string name = Util.IsNullOrEmpty(scriptClass.className)?"Object":scriptClass.className;
					string classPath = Util.IsNullOrEmpty(scriptClass.package)?scriptClass.className:scriptClass.package+"::"+scriptClass.className;
					return classPath;
				}
				else
				{
					return "Object";
				}
			}
		}
		private class trace : ScorpioHandle
		{
			public object Call(ScriptObject[] parameters)
			{
				for (int i = 0; i < parameters.Length; ++i) 
				{
					Debug.Log(parameters[i].ToString());
				}
				return null;
			}
		}
		private class import_type : ScorpioHandle
		{
			public import_type()
			{
				
			}
			public object Call(ScriptObject[] parameters)
			{
				ScriptString str = parameters[0] as ScriptString;
				if (str == null) throw new ExecutionException("import_type 参数必须是 string");
				return Util.LoadType(str.Value);
			}
		}
		private class getTimer:ScorpioHandle
		{
			public object Call(ScriptObject[] parameters)
			{
				return Time.time * 1000;
			}
		}
		private class getDefinitionByName:ScorpioHandle
		{
			public object Call(ScriptObject[] parameters)
			{
				string name = parameters[0].ObjectValue.ToString();
				return Util.GetClass(name);
			}
		}
	}
}

